<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Game
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 
 
 
class Game_Plugin_Distributor_Kongregate extends Game_Plugin_Distributor_Abstract
{
  protected $_type_name = 'kongregate';
  /*
  `game_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  
  `distributor_type` varchar(64) NULL,
  `distributor_game_id` varchar(128) NULL,
  `distributor_gameplays` int(11) unsigned NOT NULL DEFAULT '0',
  `distributor_rating` varchar(128) NULL,

  `developer_name` varchar(128) NULL, 
  
  `user_id` int(11) unsigned NOT NULL,
  `category_id` int(11) unsigned NOT NULL DEFAULT '0',
  
  `photo_id` int(11) unsigned NOT NULL DEFAULT '0',
  
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `instructions` text NULL,
  `controls` text NULL,
  
  `keywords` varchar(255) DEFAULT NULL,
  
  `height` int(11) unsigned NOT NULL DEFAULT '0',
  `width` int(11) unsigned NOT NULL DEFAULT '0',
  
  `swf_url` varchar(255) DEFAULT NULL,
  `thumbnail_url` varchar(255) DEFAULT NULL,
  
  `screenshot1_url` varchar(255) DEFAULT NULL,
  `screenshot2_url` varchar(255) DEFAULT NULL,
  `screenshot3_url` varchar(255) DEFAULT NULL,
  `screenshot4_url` varchar(255) DEFAULT NULL,
  
  `leaderboard` tinyint(1) NOT NULL DEFAULT '0',
  `coin` tinyint(1) NOT NULL DEFAULT '0',
  `highscore_type` varchar(255) DEFAULT NULL,
  
  `status` varchar(32) NOT NULL DEFAULT 'pending',

  `launch_date` datetime NULL,

   */
  protected $_dataMap = array(
    'id' => 'distributor_ref_id',
    'title' => 'title',
    'thumbnail' => 'thumbnail_url',
    'launch_date' => 'launch_date',
   //'category' => 'category'
    'flash_file' => 'swf_url',
    'width' => 'width',
    'height' => 'height',
    'description' => 'description',
    'instructions' => 'instructions', // text || SimpleXmlObject
    'developer_name' => 'developer_name',
    'gameplays' => 'distributor_gameplays',
    'rating' => 'distributor_rating',
    'url' => 'distributor_game_url',
  );


  protected $_categoryMap = array(
    'Action'  => Game_Model_Category::TYPE_ACTION,
    'Multiplayer' => Game_Model_Category::TYPE_MULTIPLAYER,
    'Shooter' => Game_Model_Category::TYPE_SHOOTING,
    'Adventure & RPG' => Game_Model_Category::TYPE_ADVENTURE,
    'Sports & Racing' => Game_Model_Category::TYPE_SPORTS,
    'Strategy & Defense' => Game_Model_Category::TYPE_STRATEGY,
    'Puzzle' => Game_Model_Category::TYPE_PUZZLES,
    'Music & More' => Game_Model_Category::TYPE_RHYTHM,
    'Tutorials' => Game_Model_Category::TYPE_EDUCATION,
  );
  
  public function fetchGames()
  {
    $url = $this->getFeedUrl();
    $feed = $this->getFeed($url);

    $data = simplexml_load_string($feed);
    
    /*
    echo "<pre>";
    foreach ($data->game as $x) {
    var_dump($x);
    }
    echo "</pre>";
    exit;
    */
    
    $offset = (int) $this->_config['offset'];
    $limit = (int) $this->_config['limit'];
    
    $games = array();
    
    $i = 0;
    $cnt = 0;
    foreach ($data->game as $xmlObject) {
      $i++;
      if ($i <= $offset) {
        continue;
      }
      
      $cnt++;
      
      if ($cnt > $limit) {
        break;
      }
      
      $source = $this->xml2array($xmlObject);
      $values = $this->normalizeData($source);
      $games[] = $values;
    }
    
    return $games;
    
      //  print_r($data);
    
  }
  
  
  public function normalizeData($source)
  {
    $data = parent::normalizeData($source);
    $data['categories'] = $this->normalizeCategory(array($source["category"]));
    
    
    if (is_array($data['instructions'])) {
      unset($data['instructions']);
    }

    $data['content_rating'] = $this->_config['content_rating'];
    $data['status'] = $this->_config['status'];
    $data['keywords'] = '';
    
    
    return $data;
  }


}
